﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _3D
{
    class BlocoTxt : GameObject3D
    {
        VertexPositionTexture[] verts;
        VertexBuffer vertexBuffer;
        BasicEffect effect;
        Texture2D txt;
        

        public BlocoTxt(float size, GraphicsDevice graphics, Camera camera, Texture2D txt, Vector3 position)
        {
            this.txt = txt;
            verts = new VertexPositionTexture[14];
            verts[0] = new VertexPositionTexture(new Vector3(-size + position.X, size + position.Y, size + position.Z), new Vector2(1,0));
            verts[1] = new VertexPositionTexture(new Vector3(size + position.X, size + position.Y, size + position.Z), new Vector2( 0,1));
            verts[2] = new VertexPositionTexture(new Vector3(-size + position.X, -size + position.Y, size + position.Z), new Vector2(1, 1));
            verts[3] = new VertexPositionTexture(new Vector3(size + position.X, -size + position.Y, size + position.Z), new Vector2(1, 0));
            verts[4] = new VertexPositionTexture(new Vector3(size + position.X, -size + position.Y, -size + position.Z), new Vector2( 0,1));
            verts[5] = new VertexPositionTexture(new Vector3(size + position.X, size + position.Y, size + position.Z), new Vector2(1, 1));
            verts[6] = new VertexPositionTexture(new Vector3(size + position.X, size + position.Y, -size + position.Z), new Vector2(1, 0));
            verts[7] = new VertexPositionTexture(new Vector3(-size + position.X, size + position.Y, size + position.Z), new Vector2( 0,1));
            verts[8] = new VertexPositionTexture(new Vector3(-size + position.X, size + position.Y, -size + position.Z), new Vector2(1, 1));
            verts[9] = new VertexPositionTexture(new Vector3(-size + position.X, -size + position.Y, size + position.Z), new Vector2(1, 0));
            verts[10] = new VertexPositionTexture(new Vector3(-size + position.X, -size + position.Y, -size + position.Z), new Vector2(0, 1));
            verts[11] = new VertexPositionTexture(new Vector3(size + position.X, -size + position.Y, -size + position.Z), new Vector2(1, 1));
            verts[12] = new VertexPositionTexture(new Vector3(-size + position.X, size + position.Y, -size + position.Z), new Vector2(1, 0));
            verts[13] = new VertexPositionTexture(new Vector3(size + position.X, size + position.Y, -size + position.Z), new Vector2(0, 1));

            vertexBuffer = new VertexBuffer(graphics, typeof(VertexPositionTexture), verts.Length, BufferUsage.None);
            vertexBuffer.SetData<VertexPositionTexture>(verts);

            effect = new BasicEffect(graphics);
            effect.TextureEnabled = true;
            effect.Texture = txt;

            bBox = new BoundingBox(new Vector3(-2 + position.X, -2 + position.Y, -2 + position.Z),
                                        new Vector3(2 + position.X, 2 + position.Y, 2 + position.Z));
        }
        public override void Update(Camera camera)
        {
            effect.View = camera.view;
            effect.Projection = camera.projection;
            effect.World = Matrix.Identity;

            base.Update(camera);
        }

        public override void Draw(GraphicsDevice graphics)
        {
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();

                graphics.SetVertexBuffer(vertexBuffer);
                graphics.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, verts, 0, 12);
            }

            base.Draw(graphics);
        }
    }

    
}
